#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define BUFSIZE 32

int mycopy(int fd1, int fd2);
int main(int argc, char *argv[])
{
	if(argc < 3)
		return 1;

	int fd1 = open(argv[1], O_RDONLY);
	if(fd1 == -1)
		return 1;
	int fd2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666);
	if(fd2 == -1){
		close(fd1);
		return 1;
	}

	mycopy(fd1, fd2);

	close(fd1);
	close(fd2);
	return 0;
}

int mycopy(int fd1, int fd2)
{
	char buf[BUFSIZE] = {};
	int cnt;

	while(1){
		cnt = read(fd1, buf, BUFSIZE);
		if(cnt == 0){
			break;
		}else if(cnt == -1){
			return -1;
		}
		write(fd2, buf, cnt);
	}

	return 0;
}
